package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.CategoryDao;
import com.example.child_protection.dto.info.CategoryDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.CategoryRequestDTO;
import com.example.child_protection.dto.response.CategoryListResponseDTO;
import com.example.child_protection.dto.response.CategoryResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.CategoryModel;
import com.example.child_protection.service.CategoryQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 科室查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class CategoryQueryServiceImpl extends MyBaseService implements CategoryQueryService {

    @Autowired
    public CategoryDao categotyDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public CategoryResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CATEGORY_FIND_ID, requestDTO, CategoryResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, CategoryResponseDTO>() {
            @Override
            protected CategoryResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                CategoryResponseDTO responseDTO = new CategoryResponseDTO();
                CategoryModel categotyModel = categotyDao.findId(requestDTO.getId());
                CategoryDTO categoty = new CategoryDTO();
                BeanUtils.copyProperties(categotyModel, categoty);
                responseDTO.setInfo(categoty);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }


    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public CategoryListResponseDTO findList(CategoryRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CATEGORY_FIND_LIST, requestDTO, CategoryListResponseDTO.class, new MyServiceExecutor<CategoryRequestDTO, CategoryListResponseDTO>() {
            @Override
            protected CategoryListResponseDTO myprocess(CategoryRequestDTO requestDTO) throws Exception {
                CategoryListResponseDTO responseDTO = new CategoryListResponseDTO();
                CategoryModel req = new CategoryModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<CategoryModel> lists = categotyDao.findPage(req);
                List<CategoryDTO> categorys = JSONObject.parseArray(JSONArray.toJSONString(lists), CategoryDTO.class);
                responseDTO.setInfos(categorys);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
